package com.namo.cchapi.util;

import cn.hutool.log.Log;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.namo.cchcommon.exception.ApiException;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

/**
 * @author WangXin
 * @date 2021/4/12 13:29
 */
@Data
@ToString
@Slf4j
public class Bmob {

    private static final String X_Bmob_Application_Id="X-Bmob-Application-Id";
    private static final String X_Bmob_REST_API_Key ="X-Bmob-REST-API-Key";


    private String appID;
    private String appKey;
    private String template;
    public Bmob(String appID, String appKey,String template) {
        this.appID = appID;
        this.appKey = appKey;
        this.template = template;
    }

    /**
     * 发送验证码
     * @param phoneNumber
     * @return
     */
    public  JsonObject sendMessage(String phoneNumber){
        Gson gson = new Gson();
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add(X_Bmob_Application_Id,appID);
        requestHeaders.add(X_Bmob_REST_API_Key,appKey);
        requestHeaders.add("Content-Type","application/json");

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("mobilePhoneNumber",phoneNumber);
        jsonObject.addProperty("template",template);

        HttpEntity<String> requestEntity = new HttpEntity<String>(jsonObject.toString(), requestHeaders);

        ResponseEntity<String> stringResponseEntity = null;
        try {
            stringResponseEntity = restTemplate.postForEntity("https://api2.bmob.cn/1/requestSmsCode", requestEntity, String.class, "");
        } catch (Exception e) {
            log.error("短信发送错误{}"+e.getMessage());
            throw new ApiException(500L,e.getMessage());
        }
        return gson.fromJson(stringResponseEntity.getBody(),JsonObject.class);
    }

    /**
     * 校验验证码
     * @param phoneNumber
     * @param code
     * @return
     */
    public  JsonObject checkCode(String phoneNumber,String code){
        Gson gson = new Gson();
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add(X_Bmob_Application_Id,appID);
        requestHeaders.add(X_Bmob_REST_API_Key,appKey);
        requestHeaders.add("Content-Type","application/json");
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("mobilePhoneNumber",phoneNumber);

        HttpEntity<String> requestEntity = new HttpEntity<String>(jsonObject.toString(), requestHeaders);
        ResponseEntity<String> stringResponseEntity = null;
        try {
            stringResponseEntity = restTemplate.postForEntity("https://api2.bmob.cn/1/verifySmsCode/"+code, requestEntity, String.class, "");
        } catch (Exception e) {
            log.error("用户验证码校验错误{}"+e.getMessage());
            throw new ApiException(500L,e.getMessage());
        }
        return gson.fromJson(stringResponseEntity.getBody(),JsonObject.class);
    }
}
